package test.demo;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.web.bind.annotation.*;
import test.demo.Result.GlobalResult;
import test.demo.Result.layui;
import test.demo.util.RedisUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


@RestController
public class sert {
    @Resource
    UserMap userMap;
    @Resource
    RedisUtil redisUtil;
    @Resource
    CarMap carMap;

    @RequestMapping("/user")
    public String seluser() {
        List<User> users = userMap.selectList(null);
        users.forEach(System.out::println);


        return JSON.toJSONString(users);

    }

    @RequestMapping(value = "/updatauser/{name}/{password}", method = RequestMethod.GET)
    public String updatauser(@PathVariable("name") String name, @Valid @PathVariable("password") String password, HttpServletRequest request) {
        String method = request.getMethod();
        System.out.println(password);
        User user = new User();
        user.setUsername(name);
        user.setPassword(password);
//        System.out.println("还行");
        int insert = userMap.insert(user);
        System.out.println(method);

        if (insert != 0) {
            return "注册成功!";

        }

        return "注册失败!";


    }


    @RequestMapping(value = "/adduser", method = RequestMethod.GET)
    public String adduser(User user) {


//        User user = new User();
//        user.setUsername(name);
        System.out.println(user);

        System.out.println("还没到");
        int insert = userMap.insert(user);

        if (insert != 0) {
            return "注册成功!";

        }

        return "注册失败!";


    }


    @RequestMapping(value = "/auser", method = RequestMethod.POST)
    public GlobalResult adu(@Valid @RequestBody User user) {


//        System.out.println(user);
        System.out.println("saddd");
        System.out.println(4 ^ 5);

        int insert = userMap.insert(user);


        if (insert != 0) {
            return GlobalResult.ok();

        }

//        User user = new User();
//        user.setUsername(name);


        return GlobalResult.error();


    }

    @RequestMapping(value = "/allcar", method = RequestMethod.GET)
    public Object seleAll(HttpServletRequest request, HttpServletResponse response) {
        String page = request.getParameter("page");
        String limit = request.getParameter("limit");
        LambdaQueryWrapper<Car> ne;
        String name = null;
        name = request.getParameter("name");

        boolean flag = name == null;


        ne = new LambdaQueryWrapper<Car>().ge(Car::getYear, "2018-1-1").notLike(Car::getName, "帝豪").notLike(Car::getName, "比亚迪").notLike(Car::getName, "宝骏").notLike(Car::getName, "北京")
                .notLike(Car::getName, "北汽").notLike(Car::getName, "江淮").notLike(Car::getName, "长城").notLike(Car::getName, "东风").notLike(Car::getName, "名爵").notLike(Car::getName, "奇瑞")
                .notLike(Car::getName, "启辰").notLike(Car::getName, "众泰").notLike(Car::getName, "中华").notLike(Car::getName, "MG").like(!flag, Car::getName, name);

        List<Car> count = carMap.selectList(ne);


        Iterator<Car> iterator = count.iterator();
        while (iterator.hasNext()) {
            Car next = iterator.next();
            Integer index = Integer.parseInt(next.getName().substring(0, 4));
            int i = index.compareTo(new Integer(2017));
            if (i < 0) {
                iterator.remove();
            }
        }

        String s = JSON.toJSONString(count);
//        System.out.println(s);
        List list = new ArrayList();
        if (!flag) {
            list = PageUtil.startPage(count, Integer.parseInt(page), Integer.parseInt(limit));
        } else {


            if (!redisUtil.hasKey("allcar")) {
                System.out.println("新建redis");
                redisUtil.set("allcar", s);
            }

//        System.out.println("redis缓存"+redisUtil.get("allcar"));
            List<Car> allcar = JSON.parseArray(redisUtil.get("allcar").toString(), Car.class);


            System.out.println("redis缓存" + allcar);
//        List<Car> allcar = JSONObject.parseArray(redisUtil.get("allcar").toString()).toJavaList(Car.class);


            list = PageUtil.startPage(allcar, Integer.parseInt(page), Integer.parseInt(limit));

        }

        layui layui = new layui();
        layui.setCount(count.size());
        layui.setCode(0);
        layui.setMsg("Ok");
        layui.setData(list);
//        System.out.println(layui);
        return layui;
    }
    @RequestMapping(value = "/upcar" ,method = RequestMethod.POST)
    public GlobalResult updataCar(@RequestParam String nprice,@RequestParam Integer id){

        LambdaUpdateWrapper<Car> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(Car::getId, id).set(Car::getNprice, Float.parseFloat(nprice));
        int update = carMap.update(null, lambdaUpdateWrapper);
        if (update == 0) {
           return GlobalResult.error("数据更新失败");
        } else {
            redisUtil.del("allcar");
            System.out.println("修改车辆id为"+id+"的价格为"+nprice);
            return GlobalResult.ok("数据更新成功");

        }
    }
}





